package com.newegg.datafeed.adserving.adwords;

import com.google.api.ads.adwords.axis.factory.AdWordsServices;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface;
import com.google.api.ads.common.lib.auth.OfflineCredentials;
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
import com.google.api.ads.common.lib.exception.ValidationException;
import com.google.api.client.auth.oauth2.Credential;
import com.newegg.datafeed.adserving.util.NeweggProxy;

public class ADWordsParent {
	static Credential oAuth2Credential;
	static AdWordsSession defaultSession;
	static AdWordsServicesInterface adWordsServices = AdWordsServices.getInstance();
	
	static{
		NeweggProxy.init(); 
		try {
			oAuth2Credential = new OfflineCredentials.Builder().forApi(Api.ADWORDS)
		              .fromFile().build().generateCredential();
			defaultSession = new AdWordsSession.Builder().fromFile().withOAuth2Credential(oAuth2Credential).build();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}
	
	protected static <T> T get(String customerID, Class<T> interfaceClass) throws ValidationException{
		AdWordsSession session = new AdWordsSession.Builder()
				.withDeveloperToken(defaultSession.getDeveloperToken())
				.withEndpoint(defaultSession.getEndpoint())
				.withClientCustomerId(customerID)
				.withUserAgent(defaultSession.getUserAgent())
				.withOAuth2Credential(oAuth2Credential)
				.build();
		return adWordsServices.get(session, interfaceClass);
	}
}
